/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.htk.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.htk.entity.HtkHmdz;
import com.thinkgem.jeesite.modules.htk.service.HtkHmdzService;

/**
 * 每日定制信息Controller
 * @author htk
 * @version 2018-04-26
 */
@Controller
@RequestMapping(value = "${adminPath}/htk/htkHmdz")
public class HtkHmdzController extends BaseController {

	@Autowired
	private HtkHmdzService htkHmdzService;
	
	@ModelAttribute
	public HtkHmdz get(@RequestParam(required=false) String id) {
		HtkHmdz entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = htkHmdzService.get(id);
		}
		if (entity == null){
			entity = new HtkHmdz();
		}
		return entity;
	}
	
	@RequiresPermissions("htk:htkHmdz:view")
	@RequestMapping(value = {"list", ""})
	public String list(HtkHmdz htkHmdz, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HtkHmdz> page = htkHmdzService.findPage(new Page<HtkHmdz>(request, response), htkHmdz); 
		model.addAttribute("page", page);
		return "modules/htk/htkHmdzList";
	}

	@RequiresPermissions("htk:htkHmdz:view")
	@RequestMapping(value = "form")
	public String form(HtkHmdz htkHmdz, Model model) {
		model.addAttribute("htkHmdz", htkHmdz);
		return "modules/htk/htkHmdzForm";
	}

	@RequiresPermissions("htk:htkHmdz:edit")
	@RequestMapping(value = "save")
	public String save(HtkHmdz htkHmdz, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, htkHmdz)){
			return form(htkHmdz, model);
		}
		htkHmdzService.save(htkHmdz);
		addMessage(redirectAttributes, "保存定制成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkHmdz/?repage";
	}
	
	@RequiresPermissions("htk:htkHmdz:edit")
	@RequestMapping(value = "delete")
	public String delete(HtkHmdz htkHmdz, RedirectAttributes redirectAttributes) {
		htkHmdzService.delete(htkHmdz);
		addMessage(redirectAttributes, "删除定制成功");
		return "redirect:"+Global.getAdminPath()+"/htk/htkHmdz/?repage";
	}
	
	@ResponseBody
	@RequestMapping(value = "formApi")
	public String formApi(HtkHmdz htkHmdz, Model model) {
		model.addAttribute("htkHmdz", htkHmdz);
		htkHmdz.setAddress("http://baidu.com");
		return "modules/htk/htkHmdzForm";
	}
	@ResponseBody
	@RequestMapping(value = "/api/htdzData")
	public List<Map<String, Object>> treeData( HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<HtkHmdz> list = htkHmdzService.findList(new HtkHmdz());
		for (int i=0; i<list.size(); i++){
			HtkHmdz e = list.get(i);
			Map<String, Object> map = Maps.newHashMap();
			map.put("id", "u_"+e.getId());
			map.put("name", e.getName());
			map.put("content", e.getAddress());
			map.put("times", e.getFav());
			map.put("url", e.getJob());
			map.put("type", e.getTopic());
			map.put("con", e.getConstellation());
			mapList.add(map);
		}
		return mapList;
	}


}